/**
 * Created by admin on 2016/7/21.
 */
var MongoClient = require('mongodb').MongoClient;
var bson = require('bson');
var database = null;
MongoClient.connect('mongodb://120.55.114.198:4000/healthManage', function(err, db) {
    if (err) {
        throw err;
    }
    database = db;
});
exports.getHealthPlans = function (req, res) {
    database.collection('HMHealthPlan').find({},{'_id': 0}).toArray(function (error, result) {
        if (error){
            console.log(error)
        }
        res.send(result);
    })

};

exports.getHealthPlan = function (req, res) {
    database.collection('HMHealthPlan').find({'_id': bson.ObjectId(req.params.planId)},{'_id': 0}).toArray(function (error, result) {
        if (error){
            console.log(error)
        };
        res.send(result);
    })
};

exports.getBmi = function (req, res) {
    res.send('bmi')
};

exports.addBmi = function (req, res) {
    res.send('post method heath plan');
    console.log(req.body.name, req.body.passwd);
};

exports.getMain = function (req, res) {
    res.send('main')
};

exports.getBloodPressure = function (req, res) {
    database.collection('HMBloodPressure').find({'role': bson.ObjectId(req.params.rid)},{'_id': 0}).toArray(function (error, result) {
        if (error){
            console.log(error)
        }
        res.send(result);
    })
};

exports.addBloodPressure = function (req, res) {
    console.log(req.body);
    res.send('add success');
};

exports.getBloodSugar = function (req, res) {
    database.collection('HMBloodSugar').find({'role': bson.ObjectId(req.params.rid)},{'_id': 0}).toArray(function (error, result) {
        if (error){
            console.log(error)
        };
        res.send(result);
    })
};

exports.addBloodSugar = function (req, res) {
    database.collection('HMBloodSugar').insert({'role': bson.ObjectId(req.body['rid']), 'bloodSugarValue': req.body['value'], 'create_at': Date()}, function (error, reslut) {
        if (error){

        }else {
            res.send('add success')
        }
    })
};

exports.getRecentMeasure = function (req, res) {
    res.send('getRecentMeasure');
};

exports.getHealthSuggest = function (req, res) {
    res.send('getHealthSuggest');
};

exports.getHealthService = function (req, res) {
    res.send('getHealthService');
};

exports.getRoleList = function (req, res) {
    database.collection('HMRoleTemplate').find({},{'_id':0}).toArray(function(err, result) {
        if (err) {
            throw err;
        }
        console.log(result);
        res.send(result)
    });
};